package org.jbit.buy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javassist.compiler.ast.Variable;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jbit.buy.biz.UserBiz;
import org.jbit.buy.biz.easybuy_newsBiz;
import org.jbit.buy.bizImpl.UserBizImpl;
import org.jbit.buy.bizImpl.easybuy_newsBizImpl;
import org.jbit.buy.entity.User;
import org.jbit.buy.entity.easybuy_news;
import org.jbit.buy.util.Page;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;



public class easybuynews extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Page pg=new Page();
			request.setCharacterEncoding("UTF-8");
			response.setContentType("text/html;charset=UTF-8");
			String path = request.getContextPath();
			HttpSession session=request.getSession();
			PrintWriter writer=response.getWriter();
			ServletContext application = this.getServletContext();
			 User uu=new User();
			 easybuy_newsBiz enb=new easybuy_newsBizImpl();
			   String opr = request.getParameter("opr");
			   System.out.println("opr"+opr);
			   String result="";
			   String da="";
			 if (opr.equals("easybuy_news_title_aside")) {
				    List<easybuy_news> listNewsNtitle=enb.findAllNews();
				    System.out.println("个数为"+listNewsNtitle.size());
				    System.out.println("进入获取index.jsp页面aside的最新资讯");
				    if(listNewsNtitle.size()>0){
				    	result="succes";
				    	for(easybuy_news ee:listNewsNtitle){
				    		da+=ee.getTitle()+"?";
				    		
				    	}
				    	writer.print("[{\"result\":\"" + result +"\", \"data\":\""+da+"\"}]");
					
				    	
				    	
				    }else {
						result="获取失败新闻标题";
						writer.print("[{\"result\":\"" + result +"\"}]");
					}
					
				 
				 
				 
			 }else if(opr.equals("MoreNtitle")){
				  String pageIndex1=request.getParameter("pageIndex");
		
				    System.out.println("进入获取MoreNtitle.jsp页面获得全部资讯");
			
				    if(pageIndex1==null){
					  	pageIndex1="1";
					  }
					  int pageIndex=Integer.parseInt(pageIndex1);
					   int totalcount=enb.getTotslCount();
					  int currPageNo=pageIndex;
					  if(currPageNo<1){
					  	currPageNo=1;
					  }
					  pg.setCurrPageNo(currPageNo);
					  pg.setPageSize(10);
					  pg.setTotalCount(totalcount);
					  List<easybuy_news> newsList =enb.getAllNewsByPage(pg.getCurrPageNo(), pg.getPageSize());
					  pg.setNewsList(newsList);
	                  //1表示是首页的分页
//					  pg.setZhonglei(1);
					  String json=JSON.toJSONString(pg, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero,SerializerFeature.WriteNullStringAsEmpty);  
					  writer.print(json);
			
				    
				    	
				    	
				    
				 
				 
				 
			 }

			
			   
			 
			 
			 
			 writer.flush();
			 writer.close();
		} catch (Exception e) {
			
			e.printStackTrace();
		}

}
}
